<?php
// 本类由系统自动生成，仅供测试用途
class MagicAction extends MemberAction {
    public function index(){
	   
		if($this->_get('Magic_id')){
			session('Magic_id',$this->_get('Magic_id'));
		}
		if(!isset($GET['Magic_id']) && !session('Magic_id')){
			$this->error('参数错误');
		}
		$m=M('Magic');
		$arr=$m->where(array('site_id' => __SITEID__, 'id' => session('Magic_id')))->find();
		if(!is_array($arr)){
			$this->error('抽奖活动已经结束或不存在这个活动');	
		}
		$arr['config'] = unserialize($arr['config']);
		$this->assign('arr',$arr);
		$this->display();
	}
	public function index1(){
	    
		if($this->_get('Magic_id')){
			session('Magic_id',$this->_get('Magic_id'));
		}
		if(!isset($GET['Magic_id']) && !session('Magic_id')){
			$this->error('参数错误');
		}
		$m=M('Magic');
		$arr=$m->where(array('site_id' => __SITEID__, 'id' => session('Magic_id')))->find();
		if(!is_array($arr)){
			$this->error('抽奖活动已经结束或不存在这个活动');	
		}
		$arr['config'] = unserialize($arr['config']);
		$this->assign('arr',$arr);
		$this->display();
	}
    public function run(){
    		$prize_arr=array();
    		$m=M('Magic');
    		$arr=$m->where(array('site_id' => __SITEID__, 'id' => session('Magic_id')))->find();
			$arr=unserialize($arr['config']);
			//dump($arr);
    		foreach($arr as $key=>$val){
				$min=explode(",",$val['min']);
				$max=explode(",",$val['max']);
				if(count($min)>1){
					$val['min']=$min;
				}
				if(count($max)>1){
					$val['max']=$max;
				}
					$prize_arr[$key]=$val; 
			}
			//dump($prize_arr);
			echo $this->getResult($prize_arr);
    }
    private function getResult($priearr){
		$arr=array();
		$count=array();
		foreach ($priearr as $key => $val) {
    		$arr[$val['id']] = $val['chance'];
    		$count[$val['id']] = $val['praisenumber'];
		}
		$rid = $this->getRand($arr,$count); //根据概率获取奖项id 
		$res = $priearr[$rid-1]; //中奖项
		$username=$this->memberInfo['username'];
		$password=$this->memberInfo['password'];
		$m=M('Member');
		$row=$m->alias('u')->field('u.id,n.number')->join('inner join __MAGICUSERADDNUMBER__ as n on u.id=n.aid')->where(array('u.username'=>$username,'u.password'=>$password, 'magic_id' => session('Magic_id')))->find();//dump($row);
		if($row){
			if($row['number']==0){
				$num=0;
				$result['praisename'] =null;
				$result['angle']=0;
			}else{
				$num=$row['number']-1;
				$min = $res['min'];
				$max = $res['max'];
				if(is_array($min)){ //多等奖的时候
    				$i = mt_rand(0,count($min)-1);
    				$result['angle'] = mt_rand($min[$i],$max[$i]);
				}else{
    				$result['angle'] = mt_rand($min,$max); //随机生成一个角度
				}
				$result['praisename'] = $res['praisename'];
			}
			//用户抽奖次数减1
			$cjModel=M('Magicuseraddnumber');
			$cjModel->where(array('aid'=>$row['id'],'magic_id' => session('Magic_id')))->save(array('number'=>$num));
			$result['num']=$num;
			//用户抽取的那个奖项减1
			$mode=M('Magic');
			$row = $mode->where(array('site_id' => __SITEID__, 'id' => session('Magic_id')))->find();
			$row = unserialize($row['config']);
			if($row[$res['id']-1]['praisenumber'] == -1){
				$row[$res['id']-1]['praisenumber']=-1;
			}else if($row[$res['id']-1]['praisenumber']==0){
				$row[$res['id']-1]['praisenumber']=0;
			}else{
				$row[$res['id']-1]['praisenumber']--;
			}
			if($row[$res['id']-1]['praisenumber']<=0){
				$row[$res['id']-1]['chance'] = 0;
			}
			
			$mode->where(array('site_id' => __SITEID__, 'id' => session('Magic_id')))->save(array('config'=>serialize($row)));
			//写入中奖记录
			$dj['aid'] = $this->memberInfo['id'];
			$dj['magic_id'] = session('Magic_id');
			$dj['prizeid'] = $res['id'];
			M('Magicuseraddorder')->data($dj)->add();
			
			return $this->json($result);
		}
    }
    private function getRand($proArr,$proCount){
    	$result = '';
    	$proSum=0;
    	//概率数组的总概率精度  获取库存不为0的
    	foreach($proCount as $key=>$val){
    		if($val==0){
    			continue;
    		}else{
    			$proSum=$proSum+$proArr[$key];
    		}
    	}
    	//概率数组循环 �
    	foreach ($proArr as $key => $proCur) {
    		if($proCount[$key]==0){
    			continue;
    		}else{
    			$randNum = mt_rand(1, $proSum);//关键
        		if ($randNum <= $proCur) {
        			$result = $key;
           	 		break;
        		}else{
            		$proSum -= $proCur;
        		}
    		}

    	}
    	unset ($proArr);
    	return $result;
    }
    private function json($array){
    	$this->arrayRecursive($array, 'urlencode', true);
		$json = json_encode($array);
		return urldecode($json);
    }
    //对数组中所有元素做处理
	private function arrayRecursive(&$array, $function, $apply_to_keys_also = false){
		foreach ($array as $key => $value) {
			if (is_array($value)) {
				arrayRecursive($array[$key], $function, $apply_to_keys_also);
			}else{
				$array[$key] = $function($value);
			}
			if ($apply_to_keys_also && is_string($key)){
				$new_key = $function($key);
				if ($new_key != $key){
					$array[$new_key] = $array[$key];
					unset($array[$key]);
				}
			}
		}
	}
}